#!/bin/sh
### BEGIN INIT INFO
# Provides:          nscd
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts the Name Service Cache Daemon
### END INIT INFO

#
# nscd:		Starts the Name Service Cache Daemon
#
# description:  This is a daemon which handles passwd and group lookups
#		for running programs and caches the results for the next
#		query.  You should start this daemon only if you use
#		slow Services like NIS or NIS+

PATH="/sbin:/usr/sbin:/bin:/usr/bin"
NAME="nscd"
DESC="Name Service Cache Daemon"
DAEMON="/usr/sbin/nscd"
PIDFILE="/var/run/nscd/nscd.pid"

# Sanity checks.
umask 022
[ -f /etc/nscd.conf ] || exit 0
[ -x "$DAEMON" ] || exit 0
[ -d /var/run/nscd ] || mkdir -p /var/run/nscd
. /lib/lsb/init-functions

start_nscd()
{
	# Return
	#   0 if daemon has been started or was already running
	#   2 if daemon could not be started
	start-stop-daemon --start --quiet --pidfile "$PIDFILE" --exec "$DAEMON" --test > /dev/null || return 0
	start-stop-daemon --start --quiet --pidfile "$PIDFILE" --exec "$DAEMON" || return 2
}

stop_nscd()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped

	# we try to stop using nscd --shutdown, that fails also if nscd is not present.
	# in that case, fallback to "good old methods"
	RETVAL=0
	if ! $DAEMON --shutdown; then
		start-stop-daemon --stop --quiet --pidfile "$PIDFILE" --name "$NAME" --test > /dev/null
		RETVAL="$?"
		[ "$?" -ne 0  -a  "$?" -ne 1 ] && return 2
	fi

	# Wait for children to finish too
	start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec "$DAEMON" > /dev/null
	[ "$?" -ne 0  -a  "$?" -ne 1 ] && return 2
	rm -f "$PIDFILE"
	return "$RETVAL"
}

status()
{
	# Return
	#   0 if daemon is stopped
	#   1 if daemon is running
	start-stop-daemon --start --quiet --pidfile "$PIDFILE" --exec "$DAEMON" --test > /dev/null || return 1
	return 0
}

invalidate_cache()
{
	for table in passwd group hosts services netgroup ; do
		$DAEMON --invalidate $table
	done
}

case "$1" in
start)
	log_daemon_msg "Starting $DESC" "$NAME"
	start_nscd
	case "$?" in
		0) invalidate_cache ; log_end_msg 0 ; exit 0 ;;
		1) log_warning_msg " (already running)." ; exit 0 ;;
		*) log_end_msg 1 ; exit 1 ;;
	esac
	;;
stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
	stop_nscd
	case "$?" in
		0) log_end_msg 0 ; exit 0 ;;
		1) log_warning_msg " (not running)." ; exit 0 ;;
		*) log_end_msg 1 ; exit 1 ;;
	esac
	;;
restart|force-reload)
	log_daemon_msg "Restarting $DESC" "$NAME"
	invalidate_cache
	stop_nscd
	case "$?" in
	0|1)
		start_nscd
		case "$?" in
			0) log_end_msg 0 ; exit 0 ;;
			1) log_failure_msg " (failed -- old process is still running)." ; exit 1 ;;
			*) log_failure_msg " (failed to start)." ; exit 1 ;;
		esac
		;;
	*)
		log_failure_msg " (failed to stop)."
		exit 1
		;;
	esac
	;;
try-restart)
	log_daemon_msg "Trying to restart $DESC" "$NAME"
	stop_nscd
	case "$?" in
		0)
			start_nscd
			case "$?" in
				0) invalidate_cache ; log_end_msg 0 ; exit 0 ;;
				*) log_failure_msg " (failed to start)." ; exit 1 ;;
			esac
			;;
		1) log_success_msg "(not running)." ; exit 0 ;;
		*) log_failure_msg "(failed to stop)." ; exit 1 ;;
	esac
	;;
status)
	log_daemon_msg "Status of $DESC service: "
	status
	case "$?" in
		0) log_failure_msg "not running." ; exit 3 ;;
		1) log_success_msg "running." ; exit 0 ;;
	esac
	;;
*)
	echo "Usage: /etc/init.d/$NAME {start|stop|force-reload|restart|status}" >&2
	exit 1
	;;
esac

